__author__ = 'East'

import re

res = re.match('^Chen\d+', 'Chen1232Ronghua123')
print(res)
print(res.group())

print('------------------------------------')
res = re.match('^.+', 'Chen1232Ronghua123')
print(res)

print('------------------------------------')
res = re.match('^R.+a$', 'Chen1232Ronghua123')   #  match是从字符串开头开始
print(res)

print('------------------------------------')
res = re.search('R.+', 'Chen1232Ronghua123')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.search('R.+$', 'Chen1232Ronghua123a')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.search('R.+a', 'Chen1232Ronghua123')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.search('R[a-zA-Z]+a', 'Chen1232Ronghua123')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.search('#.+#', '1234#hello#')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.search('a?', 'aalex')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.search('[0-9]{3}', 'a1a2l345ex')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.findall('[0-9]{1,3}', 'a1a2l345ex')   #  findall从整个文本中搜索 找到所有满足的
print(res)

print('------------------------------------')
res = re.findall('abc|ABC', 'ABCBabcCD')   #  search从整个文本中搜索
print(res)

print('------------------------------------')
res = re.search('(abc){2}\|', 'abcabc|ds')   #  search从整个文本中搜索
print(res)

# 高级装逼
print('------------------------------------')
res = re.search('(?P<id>[0-9]+)', 'abcd1234daf@34').groupdict()     # 形成一个字典
print(res)

print('------------------------------------')
res = re.search('(?P<id>[0-9]+)(?P<name>[a-zA-Z]+)', 'abcd1234daf@34').groupdict()     # 形成一个字典
print(res)

print('------------------------------------')
res = re.search('(?P<id>[0-9]+)(?P<name>[a-zA-Z]+)', 'abcd1234daf@34').group('id')     # 形成一个字典
print(res)

# 应用 身份证
print('------------------------------------')
res = re.search('(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})', '37148199310240234')
print(res.groupdict())

print('------------------------------------')
res = re.split('[0-9]+', 'abc12de3f45Gh')
print(res)

# 将串中的数字换成|
print('------------------------------------')
res = re.sub('[0-9]+', '|', 'abc12de3f45Gh')
print(res)

# 将串中的数字换成|, 只换前两个
print('------------------------------------')
res = re.sub(r'[0-9]+', '|', 'abc12de3f45Gh', count=2)
print(res)

# 匹配反斜杠
print('------------------------------------')
res = re.search(r'\\', 'abc12de\f45Gh')
print(res)

